#include "Actor.h"

Actor::Actor()
{
    //初始化坐标
    x = 0;
    y = 0;
    nowFrameIndex = 0.0;
    //初始化速率
    xVel = 0;
    yVel = 0;

    setClips();
}



void Actor::handle_input1(SDL_Event event)
{
    //当一个键被按下
    if( event.type == SDL_KEYDOWN )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_w)
        {
            yVel -= ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_s)
        {
            yVel += ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_a)
        {
            xVel -= ACTOR_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_d)
        {
            xVel += ACTOR_WIDTH / 2;
        }
    }
    //当一个键盘按键被松开
    else if( event.type == SDL_KEYUP )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_w)
        {
            yVel += ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_s)
        {
            yVel -= ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_a)
        {
            xVel += ACTOR_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_d)
        {
            xVel -= ACTOR_WIDTH / 2;
        }
    }
}


void Actor::handle_input(SDL_Event event)
{
    //当一个键被按下
    if( event.type == SDL_KEYDOWN )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_UP)
        {
            yVel -= ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_DOWN)
        {
            yVel += ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_LEFT)
        {
            xVel -= ACTOR_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_RIGHT)
        {
            xVel += ACTOR_WIDTH / 2;
        }
    }
    //当一个键盘按键被松开
    else if( event.type == SDL_KEYUP )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_UP)
        {
            yVel += ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_DOWN)
        {
            yVel -= ACTOR_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_LEFT)
        {
            xVel += ACTOR_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_RIGHT)
        {
            xVel -= ACTOR_WIDTH / 2;
        }
    }
}


void Actor::move()
{
    //将圆点左右移动
    x += xVel;
    //将圆点上下移动
    y += yVel;
}

void Actor::show(SDL_Surface *screen)
{
     
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    nowFrameIndex += 0.2;
    if(nowFrameIndex >= FRAME_COUNT)
    {
        nowFrameIndex = 0;
    }
    
    SDL_BlitSurface( background, &clips[(int)nowFrameIndex], screen, &offset );
}


void Actor::setBackground(SDL_Surface *background)
{
    this->background = background;
}


void Actor::setPos(int x, int y)
{
    this->x = x;
    this->y = y;
}
void Actor::getPos(int &x, int &y)
{
    x = this->x;
    y = this->y;
}


void Actor::adaptPos(int w, int h)
{
    if( this->x < 0 )
    {
        if( camera != NULL ) camera->x += this->x;
        this->x = 0;
    }
    else if( this->x > (w-ACTOR_WIDTH) )
    {
        if( camera != NULL ) camera->x += this->x - (w-ACTOR_WIDTH);
        this->x = w-ACTOR_WIDTH;
    }

    if( this->y < 0 )
    {
        if( camera != NULL ) camera->y += this->y;
        this->y = 0;
    }
    else if( this->y > (h-ACTOR_HEIGHT) )
    {
        if( camera != NULL ) camera->y += this->y - (h-ACTOR_HEIGHT);
        this->y = h-ACTOR_HEIGHT;
    }
}


void Actor::setClips()
{
    //剪切子图
    for ( int i = 0; i < FRAME_COUNT; i++ )
    {
        clips[ i ].x = ACTOR_WIDTH * i;
        clips[ i ].y = 0;
        clips[ i ].w = ACTOR_WIDTH;
        clips[ i ].h = ACTOR_HEIGHT;
    }
}

void Actor::setCamera(SDL_Rect *camera)
{
    this->camera = camera;
}